//
//  WYView.m
//  Brand
//
//  Created by Faker on 5/23/14.
//  Copyright (c) 2014 Faker. All rights reserved.
//

#import "WYView.h"
#import "Header.h"

@interface WYView()

@property (strong, nonatomic) IBOutlet UIView *mainView;
@property (weak, nonatomic) IBOutlet UIScrollView *myScrollView;

@property (weak, nonatomic) IBOutlet UIImageView *personBgView;
@property (weak, nonatomic) IBOutlet UIImageView *personImageView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel1;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel2;

@property (weak, nonatomic) IBOutlet UIView *infoBgView;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel1;
@property (weak, nonatomic) IBOutlet UILabel *infoLabel2;

@property (strong, nonatomic) NSArray* picArray;
@property (assign, nonatomic) int currentPicIndex;

@end

@implementation WYView

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

//如果用IB创建，则需要而外调用setUp方法。
-(id) initWithCoder:(NSCoder *)aDecoder{
    self =[super initWithCoder:aDecoder];
    if (self) {
       [[[NSBundle mainBundle] loadNibNamed:@"WYCell" owner:self options:nil] lastObject];
        
    }
    return self;
}

-(void)setUpWithDict:(NSDictionary *)dic
{
    [self addSubview:self.mainView];
    [self startAnimation];
    self.picArray = [dic objectForKey:@"page_pic"];
    [self fillDataWithDict:[self.picArray objectAtIndex:0]];
    
    self.myScrollView.contentHeight = self.myScrollView.height+1;
    if(!isiPhone5)
    {
        self.myScrollView.contentHeight = self.myScrollView.height + 100;
    }
}

-(void)fillDataWithDict:(NSDictionary*)dic
{
    [[AFGetData sharedInstance] initSDWebImage:[dic objectForKey:@"pic"] andTrueImageView:self.personImageView andPlaceholderImage:nil];
    self.titleLabel1.text = [dic objectForKey:@"cont_1"];
    self.titleLabel2.text = [dic objectForKey:@"cont_2"];
}

#pragma mark - 动画
-(void)startAnimation
{
    self.personBgView.animationImages = [NSArray arrayWithObjects:[UIImage imageNamed:@"ray1"],[UIImage imageNamed:@"ray2"], [UIImage imageNamed:@"ray1"],[UIImage imageNamed:@"ray2"],nil];
    self.personBgView.animationDuration = 1;
    self.personBgView.animationRepeatCount = CGFLOAT_MAX;
    [self.personBgView startAnimating];
}


#pragma mark - 图片的循环
-(int)validPageValue:(int)value
{
    if(value == -1) value = self.picArray.count - 1;
    if(value == self.picArray.count) value = 0;
    self.currentPicIndex = value;
    return value;
}

#pragma mark - xib点击事件
- (IBAction)goForword:(id)sender {
    [self fillDataWithDict:[self.picArray objectAtIndex:[self validPageValue:self.currentPicIndex-1]]];
}

- (IBAction)goNext:(id)sender {
     [self fillDataWithDict:[self.picArray objectAtIndex:[self validPageValue:self.currentPicIndex+1]]];
}
@end
